/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proyecto.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javax.sql.DataSource;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Analisis;

/**
 *
 * @author Administrador
 */
public class AnalisisDaoImpl extends BaseDAO implements AnalisisDao{

    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

     public Collection<Analisis> listar() throws DAOExcepcion {
        Collection<Analisis> c = new ArrayList<Analisis>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = dataSource.getConnection();
            String query = "select id_analisis,descripcion,fec_registro from tb_analisis";
            stmt = con.prepareStatement(query);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Analisis vo = new Analisis();
                vo.setId_analisis(rs.getInt("id_analisis"));
                vo.setDescripcion(rs.getString("descripcion"));
                vo.setFec_registro(rs.getDate("fec_registro"));
                c.add(vo);
            }

        } catch (SQLException e) {
            System.err.println(e.getMessage());
            throw new DAOExcepcion(e.getMessage());
        } finally {
            this.cerrarResultSet(rs);
            this.cerrarStatement(stmt);
            this.cerrarConexion(con);
        }
        return c;
    }

}
